package Main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Army.building.Building;
import Army.unit.Unit;
import Component.MyControlItem;
import GameObject.ImageObject;
import GameObject.Sprite;
import GameObject.ValueBar;
import Global.ResourceManager;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

/**
 *
 * @author Huy
 */
public class InfoPanel extends MyControlItem {

	Building building;
	static Sprite workerIcon = new Sprite(ResourceManager.getImage("icon/worker"), 1, 1, 0, 0);
	static Sprite spearmanIcon = new Sprite(ResourceManager.getImage("icon/spearman"), 1, 1, 0, 0);
	static Sprite swordmanIcon = new Sprite(ResourceManager.getImage("icon/swordman"), 1, 1, 0, 0);
	static Sprite archerIcon = new Sprite(ResourceManager.getImage("icon/archer"), 1, 1, 0, 0);
	static ValueBar bar = new ValueBar(Color.BLUE, 0, new Rectangle(0, 50, 60, 10));
	Point position;
	Point size;

	public InfoPanel(int x, int y, int w, int h) {
		position = new Point(x, y);
		size = new Point(w, h);
		sprite = new ImageObject();
	}

	@Override
	public void setScaleX(float s) {
		
		workerIcon.setScaleX(s);	
		spearmanIcon.setScaleX(s);
		swordmanIcon.setScaleX(s);
		archerIcon.setScaleX(s);
		//bar.setPosition((int) (4 * s), (int) (50 * s));
		//bar.setSize((int) (52 * s), (int) (5 * s));
		setBarSize();
	}
	@Override
	public void setScaleY(float s) {
		
		workerIcon.setScaleY(s);	
		spearmanIcon.setScaleY(s);
		swordmanIcon.setScaleY(s);
		archerIcon.setScaleY(s);
		//bar.setPosition((int) (4 * s), (int) (50 * s));
		//bar.setSize((int) (52 * s), (int) (5 * s));
		setBarSize();
	}
	private void setBarSize()
	{
		bar.setPosition((int) (4 * archerIcon.getScaleX()), (int) (50 * archerIcon.getScaleY()));
		bar.setSize((int) (52 * archerIcon.getScaleX()), (int) (5 * archerIcon.getScaleY()));
	}
	public Building getBuilding() {
		return building;
	}

	public void setBuilding(Building building) {
		this.building = building;
	}

	@Override
	public void draw(Graphics2D g) {
		if (!isVisible || building == null || !building.isAlive()) {
			return;
		}
		int n = building.getListUnits().size();
		double step = (double) (size.x -60) / (n-1);
		if(step>40)
			step = 40;
		for (int i = n - 1; i >= 0; i--) {
			Unit unit = building.getListUnits().get(i);
			Sprite sprt;
			switch (unit.getType()) {
				case UnitArcher:
					sprt = archerIcon;
					break;
				case UnitSpear:
					sprt = spearmanIcon;
					break;
				case UnitSword:
					sprt = swordmanIcon;
					break;
				default:
					sprt = workerIcon;
			}
			int x = (int) (step * i);
			sprt.setPosition(x,0);
			sprt.draw(g, position.x, position.y);
		}
		if (!building.getListUnits().isEmpty()) {
			Unit u = building.getListUnits().get(0);
			double value = (double) u.getHealthPoint() / u.getMaxHealthPoint();
			bar.setValue(value);
			bar.draw(g, (int)(position.x*workerIcon.getScaleX()), (int)(position.y*workerIcon.getScaleY()));
		}

	}
}
